package dm.repository;

import dm.ast.DmDataType;
import dm.ast.model.gen.DmConstantGen;

import java.util.ArrayList;

/**
 * @author scy
 * @date 2024/4/17
 * 元数据仓库
 * dm内部函数
 */
public class IFunction {
    public int funID;   //函数id ,对应v$ifun中id
    public String funName;  //函数名称
    public DmDataType reType;  //返回值类型
    public ArrayList<DmDataType> paras = new ArrayList<DmDataType>();  //参数列表

    public int min_paras = 0;  //最小的参数数目，部分函数参数个数可以变化，例如GREATEST
    public int max_paras = -1;   //最大的参数数目，部分函数参数个数可以变化，例如GREATEST
    public boolean runableFlag = true;

    public IFunction(int id, String name, DmDataType reType) {
        this.funID = id;
        this.funName = name;
        this.reType = reType;
    }

    public IFunction(String name, DmDataType dmDataType) {
        this.funName = name;
        this.reType = reType;
    }

    public void setRunableFlag(boolean runableFlag){
        this.runableFlag= runableFlag;
    }

    public void addPara(DmDataType para){
        paras.add(para);
    }

    public String getInfo(){
        StringBuffer out = new StringBuffer();
        out.append(String.format("%d: %s" , funID , funName));
        out.append("(");
        for (int i = 0; i < paras.size(); i++) {
            out.append(paras.get(i).toString() + ",");
        }
        if(paras.size() != 0){
            out.deleteCharAt(out.length() - 1);
        }
        out.append(") return" + reType.toString());
        return out.toString();
    }

    public String getTestSql(){
        StringBuffer out = new StringBuffer();
        out.append(String.format("select %s" , funName));
        out.append("(");
        for (int i = 0; i < paras.size(); i++) {
            out.append(DmConstantGen.gen(paras.get(i)).toString() + ",");
        }
        if(paras.size() != 0){
            out.deleteCharAt(out.length() - 1);
        }
        out.append(");");
        return out.toString();
    }
}
